package com.example.controller;

import java.util.List;

import com.example.domain.dto.result.MaterialPurchaseResult;
import com.example.factory.ResultFactory;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.domain.entity.MaterialPurchase;
import com.example.domain.query.MaterialPurchaseQuery;
import com.example.service.IMaterialPurchaseService;
import io.swagger.v3.oas.annotations.Operation;



/**
 * <p>
 * 物料采购表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2025-05-22
 */
@CrossOrigin
@RestController
@RequestMapping("/materialPurchase")
@Tag(name = "物料采购管理器    ")
public class MaterialPurchaseController {
    @Autowired
    private IMaterialPurchaseService materialPurchaseService;

    @Autowired
    private ResultFactory resultFactory;

    @GetMapping("/list")
    @Operation(summary = "获取物料采购列表")
    public List<MaterialPurchase> list() {
        return materialPurchaseService.list();
    }

    @PostMapping("/save")
    @Operation(summary = "新增物料采购")
    public boolean save(@RequestBody MaterialPurchase materialPurchase) {
        return materialPurchaseService.save(materialPurchase);
    }

    @PostMapping("/update")
    @Operation(summary = "更新物料采购")
    public boolean update(@RequestBody MaterialPurchase materialPurchase) {
        MaterialPurchase original = materialPurchaseService.getById(materialPurchase.getPurchaseId());
        if (original == null) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "采购申请不存在");
        }
        // 如果内容被修改，将审批状态重置为未审核
        if (!original.getMaterialName().equals(materialPurchase.getMaterialName()) ||
            !original.getPurchaseQuantity().equals(materialPurchase.getPurchaseQuantity()) ||
            !original.getPurchaseReason().equals(materialPurchase.getPurchaseReason()) ||
            !original.getAdditionalInfo().equals(materialPurchase.getAdditionalInfo())) {
            materialPurchase.setAuditStatus(0);
            materialPurchase.setAuditComment(null);
        }
        return materialPurchaseService.updateById(materialPurchase);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除物料采购")
    public void deleteById(@PathVariable int id) {
        if (!materialPurchaseService.removeById(id)) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "删除失败，物料不存在");
        }
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询物料采购")
    public IPage<MaterialPurchaseResult> pageQuery(@RequestBody MaterialPurchaseQuery query,
                                                   @RequestParam(defaultValue = "1") int pageNum,
                                                   @RequestParam(defaultValue = "10") int pageSize) {
        return this.resultFactory.convertMaterialPurchasePage(materialPurchaseService.pageQuery(query, pageNum, pageSize));
    }

    @PostMapping("/audit/{id}")
    @Operation(summary = "审核物料采购")
    public boolean audit(@PathVariable Integer id, @RequestParam Integer auditStatus, @RequestParam String auditComment) {
        MaterialPurchase materialPurchase = materialPurchaseService.getById(id);
        if (materialPurchase == null) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "采购申请不存在");
        }
        if (auditStatus != 1 && auditStatus != 2) {
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "无效的审批状态");
        }
        materialPurchase.setAuditStatus(auditStatus);
        materialPurchase.setAuditComment(auditComment);
        return materialPurchaseService.updateById(materialPurchase);
    }

    @PostMapping("/updateActualQuantity/{purchaseId}")
    @Operation(summary = "更新实际采购量")
    public boolean updateActualQuantity(@PathVariable Integer purchaseId, @RequestParam Integer actualQuantity) {
        MaterialPurchase materialPurchase = materialPurchaseService.getById(purchaseId);
        if (materialPurchase == null) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "采购申请不存在");
        }
        if (actualQuantity < 0) {
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "实际采购量不能为负数");
        }
        materialPurchase.setActualQuantity(actualQuantity);
        return materialPurchaseService.updateById(materialPurchase);
    }
}
